<?php
class Khachhang_AdminKhcnController extends Zendvn_Controller_Action{
	
	protected $_arrParam;
	protected $_currentController;
	protected $_actionMain;
	
	protected $_paginator = array(
									'itemCountPerPage' 	=> 15,
									'pageRange' 		=> 10,
									'currentPage' 		=> 1
								);
	protected $_namespace;
	
	protected $_page = '';
	
	public function init() {
		$this->_arrParam 			= $this->_request->getParams();
		$this->_currentController 	= '/' . $this->_arrParam['module'] . '/' . $this->_arrParam['controller'];
		$this->_actionMain 			= '/' . $this->_arrParam['module'] . '/'	. $this->_arrParam['controller'] . '/index';	

		$this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		//Lay thong tin so phan tu tren mot trang
		if (isset($this->_arrParam['limitPage'])) {
			$ssFilter->limitPage = $this->_request->getParam('limitPage');
			$this->_paginator['itemCountPerPage'] = $ssFilter->limitPage;
		}elseif (!empty($ssFilter->limitPage)) {
			$this->_paginator['itemCountPerPage'] = $ssFilter->limitPage;
		}
		
		//Trang hien tai
		if (isset($this->_arrParam['page'])) {
			$this->_paginator['currentPage'] = $this->_arrParam['page'];
			$this->_page = '/page/' . $this->_arrParam['page'];
		}
		//Trang hien tai
		if (isset($this->_arrParam['status_tiemnang'])) {
			$this->_page .= '/tn/' . $this->_arrParam['status_tiemnang'];
		}
		
		//Truyen thong tin phan trang vao mang du lieu
		$this->_arrParam['paginator'] = $this->_paginator;
		
		//$ssFilter->unsetAll();
		if (empty($ssFilter->col)) {
			$ssFilter->keywords = '';
			$ssFilter->col 		= 'kh.name';
			$ssFilter->order 	= 'ASC';
			$ssFilter->id_cat	= 0;
			$ssFilter->blocks	= 0;
		}
		$this->_arrParam['ssFilter']['keywords'] 	= $ssFilter->keywords;
		$this->_arrParam['ssFilter']['col'] 		= $ssFilter->col;
		$this->_arrParam['ssFilter']['order'] 		= $ssFilter->order;
		$this->_arrParam['ssFilter']['id_cat'] 		= $ssFilter->id_cat;
		$this->_arrParam['ssFilter']['blocks'] 		= $ssFilter->blocks;

		//Truyen ra ngoai view
		$this->view->arrParam = $this->_arrParam;
		$this->view->currentController = $this->_currentController;
		$this->view->actionMain = $this->_actionMain;
	
		$siteConfig = Zend_Registry::get('siteConfig');
		$template_path = TEMPLATE_PATH . "/admin/" . $siteConfig['template']['admin'];
		$this->loadTemplate($template_path, 'template.ini', 'customer');
	}
	
	public function addAction() {
		if ($this->_arrParam['tn'] == 0) {
			$this->view->Title = 'Thêm mới cá nhân không theo dõi';
		} else {
			$this->view->Title = 'Thêm mới cá nhân tiềm năng';
		}
		$this->view->headTitle($this->view->Title, true);
		
		$tblKhcn = new Khachhang_Model_Khcn();
		
		$tblCountries = new Default_Model_Countries();
		$this->view->slbCountries = $tblCountries->itemInSelectbox($this->_arrParam, array('task'=>'get-countries'));
		
		$tblCities = new Default_Model_Cities();
		$this->view->slbCities = $tblCities->itemInSelectbox($this->_arrParam, array('task'=>'get-cities'));
		
		$tblDistricts = new Default_Model_Districts();
		$this->view->slbDistricts = $tblDistricts->itemInSelectbox($this->_arrParam, array('task'=>'get-districts'));
		
		$tblTongiao = new Default_Model_KHTongiao();
		$this->view->slbTongiao = $tblTongiao->itemInSelectbox($this->_arrParam, array('task' => 'get-tongiao'));
		
		$tblNguonKH = new Default_Model_KHNguon();
		$this->view->slbNguonKH = $tblNguonKH->itemInSelectbox($this->_arrParam, array('task' => 'get-nguonkh'));
		
		$tblNguonKHDK = new Default_Model_KHDKNguon();
		$this->view->slbNguonKHDK = $tblNguonKHDK->itemInSelectbox($this->_arrParam, array('task' => 'get-nguonkhdk'));
		
		$tblMucSong = new Default_Model_KHMucSong();
		$this->view->slbMucSong = $tblMucSong->itemInSelectbox($this->_arrParam, array('task' => 'get-mucsongkh'));
		
		$tblQuyMoGDKH = new Default_Model_KHQuyMoGD();
		$this->view->slbQuyMoGDKH = $tblQuyMoGDKH->itemInSelectbox($this->_arrParam, array('task' => 'get-quymogdkh'));
		
		$tblHocVanKH = new Default_Model_KHHocVan();
		$this->view->slbHocVanKH = $tblHocVanKH->itemInSelectbox($this->_arrParam, array('task' => 'get-hocvankh'));
		
		$tblTruongHocKH = new Default_Model_KHTruongHoc();
		$this->view->slbTruongHocKH = $tblTruongHocKH->itemInSelectbox($this->_arrParam, array('task' => 'get-truonghockh'));
		
		$tblTTLaoDongKH = new Default_Model_KHTTLaoDong();
		$this->view->slbTTLaoDongKH = $tblTTLaoDongKH->itemInSelectbox($this->_arrParam, array('task' => 'get-ttlaodongkh'));
		
		$tblNgheNghiepKH = new Default_Model_KHNgheNghiep();
		$this->view->slbNgheNghiepKH = $tblNgheNghiepKH->itemInSelectbox($this->_arrParam, array('task' => 'get-nghenghiepkh'));
		
		$tblVaiTroKH = new Default_Model_KHVaiTro();
		$this->view->slbVaiTroKH = $tblVaiTroKH->itemInSelectbox($this->_arrParam, array('task' => 'get-vaitrokh'));
		
		$tblPhongBan = new Default_Model_Phongban();
		$this->view->slbPhongBan = $tblPhongBan->itemInSelectbox($this->_arrParam);
		
		$tblNhanVien = new Default_Model_Users();
		$this->view->slbNhanVien = $tblNhanVien->itemInSelectbox($this->_arrParam, array('task' => 'get-nhanvien'));
		
		$tblMarketing = new Marketing_Model_Item();
		$this->view->Marketing 	= $tblMarketing->listItem($this->_arrParam, array());
		
		$tblKhoahoc = new Daotao_Model_Khoahoc();
		$this->view->Khoahoc 	= $tblKhoahoc->listItem($this->_arrParam, array());
		
		$tblUser = new Default_Model_Users();
		$this->view->Users		= $tblUser->listItem(null,array('task' => 'user-all'));
		
		if ($this->_request->isPost()) {
				
			$validator = new Khachhang_Form_ValidateKhcn($this->_arrParam);
			if ($validator->isError() == true) {
				$this->view->messageError = $validator->getMessageError();
				$this->view->Item = $validator->getData();
			} else {
				$arrParam = $validator->getData();
				$lastID = $tblKhcn->saveItem($arrParam, array('task'=>'admin-add'));
				
				if(empty($this->_arrParam['type'])){
					if (isset($this->_arrParam['status_kh'])) {
						$this->_redirect('khachhang/admin-kh/index/save/ok' . $this->_page);
					} else {
						$this->_redirect('khachhang/admin-item/index/save/ok' . $this->_page);
					}
				}else{
					if($this->_arrParam['type'] == 'apply'){
						if (isset($this->_arrParam['status_kh'])) {
							$this->_redirect('khachhang/admin-kh/index/save/ok' . $this->_page);
						} else {
							$this->_redirect('khachhang/admin-khcn/edit/id/' . $lastID . $this->_page);
						}
					}
					if($this->_arrParam['type'] == 'new'){
						$this->_redirect('khachhang/admin-khcn/add' . $this->_page);
					}
				}
			}
		}
	}
	
	public function editAction() {
		if ($this->_arrParam['tn'] == 0) {
			$this->view->Title = 'Cá nhân không theo dõi :: Sửa';
		} else {
			$this->view->Title = 'Cá nhân tiềm năng :: Sửa';
		}
		$this->view->headTitle($this->view->Title, true);

		$tblKhcn = new Khachhang_Model_Khcn();
		$this->view->Item = $tblKhcn->getItem($this->_arrParam, array('task'=>'admin-edit'));
		
		if($this->view->Item['id_cat'] != 1){
			$this->_redirect('khachhang/admin-khtc/edit/id/' . $this->view->Item['id'] . '/tn/' . $this->view->Item['status_tiemnang']);
		} else {
			$tblCountries = new Default_Model_Countries();
			$this->view->slbCountries = $tblCountries->itemInSelectbox($this->_arrParam, array('task'=>'get-countries'));
			
			$tblCities = new Default_Model_Cities();
			$this->view->slbCities = $tblCities->itemInSelectbox($this->_arrParam, array('task'=>'get-cities'));
			
			$tblDistricts = new Default_Model_Districts();
			$this->view->slbDistricts = $tblDistricts->itemInSelectbox($this->_arrParam, array('task'=>'get-districts'));
			
			$tblTongiao = new Default_Model_KHTongiao();
			$this->view->slbTongiao = $tblTongiao->itemInSelectbox($this->_arrParam, array('task' => 'get-tongiao'));
			
			$tblNguonKH = new Default_Model_KHNguon();
			$this->view->slbNguonKH = $tblNguonKH->itemInSelectbox($this->_arrParam, array('task' => 'get-nguonkh'));
			
			$tblNguonKHDK = new Default_Model_KHDKNguon();
			$this->view->slbNguonKHDK = $tblNguonKHDK->itemInSelectbox($this->_arrParam, array('task' => 'get-nguonkhdk'));
			
			$tblMucSong = new Default_Model_KHMucSong();
			$this->view->slbMucSong = $tblMucSong->itemInSelectbox($this->_arrParam, array('task' => 'get-mucsongkh'));
			
			$tblQuyMoGDKH = new Default_Model_KHQuyMoGD();
			$this->view->slbQuyMoGDKH = $tblQuyMoGDKH->itemInSelectbox($this->_arrParam, array('task' => 'get-quymogdkh'));
			
			$tblHocVanKH = new Default_Model_KHHocVan();
			$this->view->slbHocVanKH = $tblHocVanKH->itemInSelectbox($this->_arrParam, array('task' => 'get-hocvankh'));
			
			$tblTruongHocKH = new Default_Model_KHTruongHoc();
			$this->view->slbTruongHocKH = $tblTruongHocKH->itemInSelectbox($this->_arrParam, array('task' => 'get-truonghockh'));
			
			$tblTTLaoDongKH = new Default_Model_KHTTLaoDong();
			$this->view->slbTTLaoDongKH = $tblTTLaoDongKH->itemInSelectbox($this->_arrParam, array('task' => 'get-ttlaodongkh'));
			
			$tblNgheNghiepKH = new Default_Model_KHNgheNghiep();
			$this->view->slbNgheNghiepKH = $tblNgheNghiepKH->itemInSelectbox($this->_arrParam, array('task' => 'get-nghenghiepkh'));
			
			$tblVaiTroKH = new Default_Model_KHVaiTro();
			$this->view->slbVaiTroKH = $tblVaiTroKH->itemInSelectbox($this->_arrParam, array('task' => 'get-vaitrokh'));
			
			$tblPhongBan = new Default_Model_Phongban();
			$this->view->slbPhongBan = $tblPhongBan->itemInSelectbox($this->_arrParam);
			
			$tblNhanVien = new Default_Model_Users();
			$this->view->slbNhanVien = $tblNhanVien->itemInSelectbox($this->_arrParam, array('task' => 'get-nhanvien'));
			
			$tblMarketing = new Marketing_Model_Item();
			$this->view->Marketing 	= $tblMarketing->listItem($this->_arrParam, array());
			
			$tblKhoahoc = new Daotao_Model_Khoahoc();
			$this->view->Khoahoc 	= $tblKhoahoc->listItem($this->_arrParam, array());
			
			$tblUser = new Default_Model_Users();
			$this->view->Users		= $tblUser->listItem(null,array('task' => 'user-all'));
			
			if ($this->_request->isPost()) {
					
				$validator = new Khachhang_Form_ValidateKhcn($this->_arrParam);
				if ($validator->isError() == true) {
					$this->view->messageError = $validator->getMessageError();
					$this->view->Item = $validator->getData();
				} else {
					$arrParam = $validator->getData();
					$lastID = $tblKhcn->saveItem($arrParam, array('task'=>'admin-edit'));
					
					if(empty($this->_arrParam['type'])){
						if (isset($this->_arrParam['status_kh'])) {
							$this->_redirect('khachhang/admin-kh/index/save/ok' . $this->_page);
						} else {
							$this->_redirect('khachhang/admin-item/index/save/ok' . $this->_page);
						}
					}else{
						if($this->_arrParam['type'] == 'apply'){
							if (isset($this->_arrParam['status_kh'])) {
								$this->_redirect('khachhang/admin-khcn/edit/id/' . $lastID . $this->_page . '/status_kh/' . $this->_arrParam['status_kh']);
							} else {
								$this->_redirect('khachhang/admin-khcn/edit/id/' . $lastID . $this->_page);
							}
						}
						if($this->_arrParam['type'] == 'new'){
							$this->_redirect('khachhang/admin-khcn/add' . $this->_page);
						}
					}
				}
			}
		}
	}
}



